﻿using BarcodeDelay.bean;
using BarcodeDelay.business;
using BarcodeDelay.FrameServer;
using BarcodeDelay.logHelp;
using JTParcrelHelper.bean;
using JTParcrelHelper.business;
using JTParcrelHelper.MessageClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SuperSocket.SocketBase;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarcodeDelay.FrameDecoder
{
    public class FrameDecoderManager
    {
        public Hashtable htWayBillHandle = new Hashtable();

        public Hashtable htWayBillTask = new Hashtable();
        public event Action curExcuteStep;

        public event Action DoneSBResponse;
        private bool isCloseJT = false;
        private string lastContent = "";
        private string lastHandleContent = "";
        private ArrayList arListLastHandle = new ArrayList();
        private bool isTestMode = true;

        private StreamWriter swb = null;
        FileStream bFile = null;
        private string priorTask = "";
        private Queue taskQue = new Queue();
        public Queue taskTodoQue = new Queue();

        public Queue batchTaskTodoQue = new Queue();

        ArrayList arListGroupName = new ArrayList();
        private DateTime lastReadGroup = System.DateTime.Now;

        public DateTime lastSendRes = System.DateTime.Now;
        private string lastHandleTasks = "";
        private Hashtable htInfo = new Hashtable();
        private Hashtable htParcelCount = new Hashtable();
        public Hashtable htParcelOutPutHistory = new Hashtable();
        private string lastDoneWaybill = "";
        private object lockQueue = new object();
        System.Threading.Thread curExcuteTask = null;

        System.Threading.Thread autoExcuteTask = null;

        System.Threading.Thread superExcuteTask = null;


        System.Threading.Thread ackExcuteTask = null;

        System.Threading.Thread heartBeatTask = null;

        System.Threading.Thread retryExcuteTask = null;



        System.Threading.Thread lanshouCheckExcuteTask = null;


        private Hashtable htTaskDoneList = new Hashtable();

        private Hashtable htReplyDupList = new Hashtable();
        private string priorTaskRecv = "";
        private DateTime dtLastSendTime = System.DateTime.Now;
        private Hashtable htDupResponseTimes = new Hashtable();
        private ArrayList arListStatics = new ArrayList();

        private Hashtable htHandleGenRecords = new Hashtable();

        private string groupName = "";
        ArrayList arListTaskFilters = new ArrayList();
        public ArrayList arListLanshouFilters = new ArrayList();

        Hashtable htRoomInfo = new Hashtable();

        public Hashtable htRoomUserWechat = new Hashtable();


        public Hashtable htRoomClose = new Hashtable();


        public Hashtable htRoomDefaultWechat = new Hashtable();

        public Hashtable htGroupUserWechat = new Hashtable();
        public Hashtable htGropRoomID = new Hashtable();
        private Hashtable htReplyCuePos = new Hashtable();
        private Hashtable htHadDoneTask = new Hashtable();
        GroupPos curGroupPosDefLanshou = new GroupPos();
        public DateTime dtLastMsg = System.DateTime.Now.AddMinutes(0);

        public Hashtable htWechatNameBean = new Hashtable();

        public Hashtable htReturnToSelfSite = new Hashtable();
        public Hashtable htStaticWriteToday = new Hashtable();
        private Hashtable htCurDay = new Hashtable();

        public Hashtable htSiteWechat = new Hashtable();

        public Queue superTaskQue = new Queue();

        public Queue ackTaskQue = new Queue();

        public Object superObj = new Object();

        public Hashtable htAllRooms = new Hashtable();

        public Hashtable htAllLanshouRooms = new Hashtable();


        public Hashtable htAllSites = new Hashtable();

        public Hashtable htAllRoomsFilters = new Hashtable();

        public Hashtable htAllowLanshouRoom = new Hashtable();

        public Hashtable htForbiddenLanshouRoom = new Hashtable();



        public Hashtable htLastRoomFilter = new Hashtable();
        public ArrayList arListNewRegSite = new ArrayList();
        public Hashtable htHasWriteStatic = new Hashtable();

        public Hashtable htRoomATClose = new Hashtable();

        public Hashtable htRoomaATEmployee = new Hashtable();
        public Hashtable htRoomWrokOrder = new Hashtable();

        public Hashtable htSiteOrderSend = new Hashtable();

        public Hashtable htSiteLastOrderTime = new Hashtable();

        public Hashtable htSiteOrderHandled = new Hashtable();

        public Hashtable htOverTime15HourSiteOrderHandled = new Hashtable();
        public Hashtable htOverTime20HourSiteOrderHandled = new Hashtable();

        public Hashtable htOverTime15HourSiteDispatchOrderHandled = new Hashtable();
        public Hashtable htOverTime20HourSiteDispatchOrderHandled = new Hashtable();


        public Hashtable htSiteLoginFaileFre = new Hashtable();

        public Hashtable htDayUserReplyFrequency = new Hashtable();

        


        public Hashtable htUnknownTask = new Hashtable();


        public DateTime dtLastQueryIntercept = System.DateTime.Now.AddMinutes(-30);


        Hashtable htTodayOutBoundProblem = new Hashtable();

        public Hashtable htSiteNameAndRoom = new Hashtable();

        public bool isFirstLoginToken = true;

        public Hashtable htRoomLanshou = new Hashtable();


        public Hashtable htInvalidToken = new Hashtable();


        public Hashtable htGroupNameRooms = new Hashtable();


        public DateTime dtLastSendLoginFail = System.DateTime.Now;


        public Hashtable htFailedParcel = new Hashtable();


        public Hashtable htTaskCheck = new Hashtable();

        DateTime dtLastCheckTask = System.DateTime.Now;

        public Hashtable htRoomCurrentWechat = new Hashtable();



        public Hashtable htDrawingRoomsUsers = new Hashtable();

        public Hashtable htCustomerServiceRoomsUsers = new Hashtable();

        public Hashtable htCodingRoomsUsers = new Hashtable();

        public Hashtable htForbiddenDeliverUsers = new Hashtable();

        public Hashtable htLanshopuRooms = new Hashtable();
        public Hashtable htGendanRooms = new Hashtable();



        public Hashtable htCommandGendan = new Hashtable();

        public Hashtable htRoomLastTriggerDraw = new Hashtable();

        public Hashtable htRoomLastTriggerCustomService = new Hashtable();

        public Hashtable htRoomLastTriggerCoding = new Hashtable();

        public Hashtable htRoomLastTriggerCheck = new Hashtable();

        public Hashtable htRoomQuesLastTriggerCheck = new Hashtable();

        public Hashtable htRoomQuesLastTriggerThank = new Hashtable();



        public Hashtable htCommandDraw = new Hashtable();
        public Hashtable htCommandCoding = new Hashtable();

        public Hashtable htCommandChangeCoding = new Hashtable();


        public Hashtable htCommandQuestion = new Hashtable();
        public Hashtable htCommandCheck = new Hashtable();

        public Hashtable hVipRooms = new Hashtable();
        public Hashtable htOffDutyTodayResponse = new Hashtable();

        public Hashtable htQuesOffDutyTodayResponse = new Hashtable();

        public Hashtable htRetryMsg = new Hashtable();

        public Hashtable htAtRetryMsg = new Hashtable();

        public string lastHandleTcpMessage = "";



        private static FrameDecoderManager instance;
        public static FrameDecoderManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrameDecoderManager();
                }
                return instance;
            }
        }

        private FrameDecoderManager()
        {
            if(!StoParams.Instance.isInitConfig)
            {
                StoParams.Instance.initParam();
            }
            if (StoParams.Instance.isWechatListen)
            {
                if (htGroupUserWechat.Count == 0)
                {
                    UtilityHelp.Instance.getUserGroup(htGroupUserWechat, htWechatNameBean);
                }
                if(htRoomUserWechat.Count==0)
                {
                    UtilityHelp.Instance.getUserGroupSuperWay(htRoomUserWechat, htWechatNameBean, htRoomDefaultWechat);

                    UtilityHelp.Instance.setUserRoomDefaultWechat(htRoomUserWechat, htRoomDefaultWechat);
                }
   

                curExcuteTask = new Thread(new ThreadStart(handleQueTask));//出港分析 开辟一个新的线程
                curExcuteTask.IsBackground = true;
                curExcuteTask.SetApartmentState(ApartmentState.STA);
                curExcuteTask.Start();

                if (!StoParams.Instance.superWechatFlag.Contains("server"))
                {
                    retryExcuteTask = new Thread(new ThreadStart(handleRetryQueTask));//出港分析 开辟一个新的线程
                    retryExcuteTask.IsBackground = true;
                    retryExcuteTask.SetApartmentState(ApartmentState.STA);
                    retryExcuteTask.Start();
                }
                    

                superExcuteTask = new Thread(new ThreadStart(handleSuperQueTask));//出港分析 开辟一个新的线程
                superExcuteTask.IsBackground = true;
                superExcuteTask.SetApartmentState(ApartmentState.STA);
                superExcuteTask.Start();

                if (StoParams.Instance.superWechatFlag.Contains("server"))
                {
                    heartBeatTask = new Thread(new ThreadStart(handleHeartBeatClient));
                    heartBeatTask.IsBackground = true;
                    heartBeatTask.SetApartmentState(ApartmentState.STA);
                    heartBeatTask.Start();


                    ackExcuteTask = new Thread(new ThreadStart(handleAckQueTask));//出港分析 开辟一个新的线程
                    ackExcuteTask.IsBackground = true;
                    ackExcuteTask.SetApartmentState(ApartmentState.STA);
                    ackExcuteTask.Start();

                    
                }
                    

            }
                
        }


        private void handleHeartBeatClient()
        {
            try
            {
                while(true)
                {
                    System.Threading.Thread.Sleep(2000);
                    JObject jObj = new JObject();
                    jObj["ServiceName"] = "heartBeatServer";
                    string strSendContend = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                    byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(strSendContend);
                    int length = contentBytes.Length;
                    byte[] arrLength = ToBytes((uint)length, 4);
                    byte[] newArr = arrLength.Concat(contentBytes).ToArray();
                    Program.weServer.sendContentToAllChannel(newArr);
                }
                


            }
            catch(Exception ex)
            {

            }
        }




 

        private void recordTodayNotice(ArrayList arListAllBarcodes)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                foreach(string strBarcode in arListAllBarcodes)
                {
                    sb.AppendLine(strBarcode);
                }
                LogHelper.Instance.writeProblemLog(sb.ToString());



            }
            catch(Exception ex)
            {

            }
        }

        private void noticeEmployee(ArrayList arListAllBarcodes, Hashtable htCustomer,string siteName)
        {
            try
            {
                foreach(DictionaryEntry de in htCustomer)
                {
                    string customerName = de.Key.ToString();
                    ArrayList arListParcel = (ArrayList)de.Value;
                    sendCustomerProblemNotice(customerName, arListParcel, siteName);
                }
            }
            catch(Exception ex)
            {

            }
        }

        private void  sendCustomerProblemNotice(string customerName, ArrayList arListParcel, string siteName)
        {
            try
            {
                if(!htSiteNameAndRoom.ContainsKey(siteName))
                {
                    LogHelper.Instance.writeLog(siteName + "没配置对应的问题件通知群");
                    return;
                }
                if(arListParcel.Count==0)
                {
                    LogHelper.Instance.writeLog(siteName + "没有需要通知的问题件");
                    return;
                }
                string roomID = htSiteNameAndRoom[siteName].ToString();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(customerName);
                foreach (ParcelBean parcel in arListParcel)
                {
                    if(htTodayOutBoundProblem.ContainsKey(parcel.wayBill))
                    {
                        continue;
                    }
                    sb.AppendLine(parcel.wayBill + " " + parcel.rebackTransferReason);
                    htTodayOutBoundProblem[parcel.wayBill] = System.DateTime.Now.ToString();
                }
                ReplyHelper.Instance.replySuperMsg(roomID, sb.ToString());
                LogHelper.Instance.writeLog("发送问题件通知："+ customerName);
                if(htTodayOutBoundProblem.Count>1000)
                {
                    htTodayOutBoundProblem.Clear();
                }
            }
            catch(Exception ex)
            {

            }
        }

        private void handleSuperQueTask()
        {
            try
            {
                while (true)
                {
                    System.Threading.Thread.Sleep(100);
                    while (superTaskQue.Count > 0)
                    {
                        try
                        {
                            WechatMsgBean wechatMsgBean = (WechatMsgBean)superTaskQue.Dequeue();
                            if(wechatMsgBean.file=="")
                            {
                                string strContent = "";
                                string msg = "";
                                JObject jObj = new JObject();
                                jObj["ServiceName"] = "wechatMsg";
                                jObj["roomid"] = wechatMsgBean.roomid;
                                jObj["content"] = wechatMsgBean.content;
                                jObj["msgtime"] = wechatMsgBean.msgtime;
                                jObj["msgtype"] = wechatMsgBean.msgtype;
                                jObj["from"] = wechatMsgBean.from;
                                jObj["isQQ"] = wechatMsgBean.isQQ;
                                jObj["MQ_robot"] = wechatMsgBean.robotQQ;

                                jObj["is_atmsg"] = wechatMsgBean.isAtMsg;

                                JArray jar = new JArray();
                                if (wechatMsgBean.atUserList.Count > 0)
                                {
                                   
                                    foreach (string userList in wechatMsgBean.atUserList)
                                    {
                                        jar.Add(userList);
                                    }
                                    jObj["atUserList"] = jar;
                                }
                                else
                                {
                                    jObj["atUserList"] = jar;
                                }
                                

                                string strSendContend = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                                byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(strSendContend);
                                int length = contentBytes.Length;
                                byte[] arrLength = ToBytes((uint)length, 4);
                                byte[] newArr = arrLength.Concat(contentBytes).ToArray();
                                Program.weServer.sendContent(newArr, wechatMsgBean.roomid, wechatMsgBean.content);
                                
                                //LogHelper.Instance.writeLog("发送客户端消息：" + strSendContend.ToString());
                                System.Threading.Thread.Sleep(50);

                            }
                            else
                            {
                                string strContent = "";
                                string msg = "";
                                JObject jObj = new JObject();
                                jObj["ServiceName"] = "wechatMsg";
                                jObj["roomid"] = wechatMsgBean.roomid;
                                jObj["content"] = wechatMsgBean.content;
                                jObj["msgtime"] = wechatMsgBean.msgtime;
                                jObj["msgtype"] = wechatMsgBean.msgtype;
                                jObj["from"] = wechatMsgBean.from;
                                jObj["isQQ"] = wechatMsgBean.isQQ;
                                jObj["MQ_robot"] = wechatMsgBean.robotQQ;
                                jObj["filename"] = wechatMsgBean.file;
                                JArray jar = new JArray();
                                if (wechatMsgBean.atUserList.Count > 0)
                                {

                                    foreach (string userList in wechatMsgBean.atUserList)
                                    {
                                        jar.Add(userList);
                                    }
                                    jObj["atUserList"] = jar;
                                }
                                else
                                {
                                    jObj["atUserList"] = jar;
                                }


                                string strSendContend = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                                byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(strSendContend);
                                int length = contentBytes.Length;
                                byte[] arrLength = ToBytes((uint)length, 4);
                                byte[] newArr = arrLength.Concat(contentBytes).ToArray();
                                Program.weServer.sendContent(newArr, wechatMsgBean.roomid, wechatMsgBean.content);
                                //LogHelper.Instance.writeLog("发送客户端消息：" + strSendContend.ToString());
                                //UtilityHelp.Instance.handleFileMsgBeans(wechatMsgBean);
                            }
                            
                            //parseCompanyTcpWechat(strContent, out msg, wechatMsgBean);
                        }
                        catch(Exception ex)
                        {
                            LogHelper.Instance.writeLog("发送客户端消息出错：" + ex.ToString());
                        }
                        

                    }







                    }
            }
            catch (Exception ex)
            {

            }
        }




        private void handleAckQueTask()
        {
            try
            {
                while (true)
                {
                    System.Threading.Thread.Sleep(200);
                    while (ackTaskQue.Count > 0)
                    {
                        try
                        {
                            WechatMsgBean wechatMsgBean = (WechatMsgBean)ackTaskQue.Dequeue();
                            if (wechatMsgBean.file == "")
                            {
                                string strContent = "";
                                string msg = "";
                                JObject jObj = new JObject();
                                jObj["ServiceName"] = "ackWechatMsg";
                                jObj["roomid"] = wechatMsgBean.roomid;
                                jObj["content"] = wechatMsgBean.content;
                                jObj["msgtime"] = wechatMsgBean.msgtime;
                                jObj["msgtype"] = wechatMsgBean.msgtype;
                                jObj["from"] = wechatMsgBean.from;
                                jObj["isQQ"] = wechatMsgBean.isQQ;
                                jObj["MQ_robot"] = wechatMsgBean.robotQQ;

                                JArray jar = new JArray();
                                if (wechatMsgBean.atUserList.Count > 0)
                                {

                                    foreach (string userList in wechatMsgBean.atUserList)
                                    {
                                        jar.Add(userList);
                                    }
                                    jObj["atUserList"] = jar;
                                }
                                else
                                {
                                    jObj["atUserList"] = jar;
                                }


                                string strSendContend = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                                byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(strSendContend);
                                int length = contentBytes.Length;
                                byte[] arrLength = ToBytes((uint)length, 4);
                                byte[] newArr = arrLength.Concat(contentBytes).ToArray();

                                if(wechatMsgBean.content.StartsWith("已通知"))
                                {
                                    continue; 
                                }
                                Program.weServer.sendContentAck(newArr, wechatMsgBean.roomid, wechatMsgBean.content);

                               
                                LogHelper.Instance.writeLog("发送Ack客户端消息：" + strSendContend.ToString());
                                System.Threading.Thread.Sleep(100);

                            }
  

                            //parseCompanyTcpWechat(strContent, out msg, wechatMsgBean);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Instance.writeLog("发送客户端消息出错：" + ex.ToString());
                        }


                    }







                }
            }
            catch (Exception ex)
            {

            }
        }



        public byte[] ToBytes(uint num, int size)
        {
            Contract.Assume(size <= 4);
            return BitConverter.GetBytes(num).Take(size).Reverse().ToArray();
        }

        public void stopThread()
        {
            try
            {
                if (curExcuteTask != null)
                {
                    curExcuteTask.Abort();
                }
                if(autoExcuteTask!=null)
                {
                    autoExcuteTask.Abort();
                }
                if (superExcuteTask != null)
                {
                    superExcuteTask.Abort();
                }
                if(retryExcuteTask!=null)
                {
                    retryExcuteTask.Abort();
                }

                if (ackExcuteTask != null)
                {
                    ackExcuteTask.Abort();
                }
                



            }
            catch(Exception ex)
            {

            }
        }

        public void handleRetryQueTask()
        {
            try
            {
                while(true)
                {
                    System.Threading.Thread.Sleep(2 * 1000);
                    try
                    {
                        ArrayList arListDel = new ArrayList();
                        ArrayList arListMsgBean = new ArrayList();

                        ArrayList arListAtMsgBean = new ArrayList();

                        foreach (DictionaryEntry de in htRetryMsg)
                        {
                            DateTime dtTask = Convert.ToDateTime(de.Key.ToString());
                            TimeSpan ts = System.DateTime.Now - dtTask;
                            if(ts.TotalSeconds>10)
                            {
                                WechatMsgBean wechatMsgBean = (WechatMsgBean)de.Value;
                                if(wechatMsgBean.retrytCount>2)
                                {
                                    arListDel.Add(de.Key.ToString());
                                    continue;
                                }
                                wechatMsgBean.retrytCount++;
                                arListMsgBean.Add(wechatMsgBean);
                                if(arListMsgBean.Count>=3)
                                {
                                    break;
                                }

                            }
                        }

                        foreach (DictionaryEntry de in htAtRetryMsg)
                        {
                            DateTime dtTask = Convert.ToDateTime(de.Key.ToString());
                            TimeSpan ts = System.DateTime.Now - dtTask;
                            if (ts.TotalSeconds > 10)
                            {
                                WechatMsgBean wechatMsgBean = (WechatMsgBean)de.Value;
                                if (wechatMsgBean.retrytCount > 2)
                                {
                                    arListDel.Add(de.Key.ToString());
                                    continue;
                                }
                                wechatMsgBean.retrytCount++;
                                arListAtMsgBean.Add(wechatMsgBean);
                                if (arListAtMsgBean.Count >= 3)
                                {
                                    break;
                                }

                            }
                        }



                        foreach (string delKey in arListDel)
                        {
                            if(htRetryMsg.ContainsKey(delKey))
                            {
                                htRetryMsg.Remove(delKey);
                            }
                        }

                        foreach(WechatMsgBean wechatMsgBean in arListMsgBean)
                        {
                            LogHelper.Instance.writeLog("重传的数据为 " + wechatMsgBean.roomid+" " + wechatMsgBean.content);
                            if(wechatMsgBean.content.Length<200)
                            {
                                ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, wechatMsgBean.content, "1", "");
                            }
                           
                        }

                        foreach (WechatMsgBean wechatMsgBean in arListAtMsgBean)
                        {
                            LogHelper.Instance.writeLog("重传的AT数据为 " + wechatMsgBean.roomid + " " + wechatMsgBean.content);
                            ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, "", "1", wechatMsgBean.atUserListString.ToString().TrimEnd(','));                           

                        }

                    }
                    catch(Exception ex)
                    {
                        LogHelper.Instance.writeLog("ex" + ex.Message);
                    }
                    

                }
            }
            catch(Exception ex)
            {

            }
        }
        

        public void handleQueTask()
        {
            try
            {
                string priorTaskKey = "";
                bool isSuperWay = false;
                while (true)
                {
                    try
                    {                      

                        try
                        {
                            System.Threading.Thread.Sleep(100);
                            arListStatics.Clear();
                            ArrayList arListToDoTask = new ArrayList();

                            lock (lockQueue)
                            {
                                string content = "";
                                string lastMsg = "";
                                TimeSpan ts = System.DateTime.Now - dtLastSendTime;
   
                                if (taskTodoQue.Count == 0)
                                {
                                    System.Threading.Thread.Sleep(200);
                                }

                                string priorWayBillAndTask = "";

                                while (taskTodoQue.Count > 0)
                                {

                                    TaskBean taskBean = (TaskBean)taskTodoQue.Dequeue();
                                    string toDoTask = taskBean.taskType;
                                    switch (toDoTask)
                                    {
                                        case "做编码":
                                            handleCoding(taskBean);
                                            break;
                                        case "改编码":
                                            handleChangeCoding(taskBean);
                                            break;
                                        case "美工":
                                            handleDrawing(taskBean);
                                            break;
                                        case "美工&编码":
                                            handleDrawing(taskBean);
                                            handleCoding(taskBean);
                                            break;
                                            
                                        case "揽收":
                                            handleLanshou(taskBean);
                                            break;
                                        case "跟单":
                                            handleGenDan(taskBean);
                                            break;
                                        case "审单":
                                            handleCheck(taskBean);
                                            break;
                                        case "疑问":
                                            handleQues(taskBean);
                                            break;
                                        case "感谢":
                                            handleThanks(taskBean);
                                            break;
                                        case "客服":
                                            handleCustomNotice(taskBean);
                                            break;

                                    }
                                }
                            }
                        }
                        catch(NullReferenceException nullexception)
                        {
                            LogHelper.Instance.writeLog("空指针 handleQueTask ex" + nullexception.StackTrace);
                        }
                        

                    }//try
                    catch (Exception ex)
                    {
                        LogHelper.Instance.writeLog("handleQueTask ex" + ex.ToString()+" 行号："+ex.StackTrace.ToString());
                    }



                }//while

            }
            catch (Exception ex)
            {

            }

        }

        private void handleQues(TaskBean taskBean)
        {
            try
            {
                string replayMsg = "亲亲稍等同事瞅瞅";
                DateTime dtNow = System.DateTime.Now;
                bool isOffDutyTime = false;
                string dutyKey = "odduty";
                if (dtNow.Hour >= 23 || dtNow.Hour <= 7)
                {
                    replayMsg = "亲亲辛苦啦，客服伙伴们休息中哦，上班给亲处理哈~";
                    isOffDutyTime = true;
                }
                if (dtNow.Hour == 22)
                {
                    if (dtNow.Minute > 30)
                    {
                        replayMsg = "亲亲辛苦啦，客服伙伴们休息中哦，上班给亲处理哈~";
                        isOffDutyTime = true;
                    }
                }
                if (dtNow.Hour == 8)
                {
                    if (dtNow.Minute < 30)
                    {
                        replayMsg = "亲亲辛苦啦，客服伙伴们休息中哦，上班给亲处理哈~";
                        isOffDutyTime = true;
                    }
                }
                string shortDateKey = System.DateTime.Now.ToShortDateString();
                bool canRespose = false;
                if(isOffDutyTime)
                {
                    dutyKey = "offduty";
                }
                
               

                if (!htQuesOffDutyTodayResponse.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    canRespose = false;
                    Hashtable htCurRoom = new Hashtable();
                    htCurRoom[shortDateKey] = dutyKey;
                    htQuesOffDutyTodayResponse[taskBean.wechatMsgBean.roomid] = htCurRoom;

                }
                else
                {
                    Hashtable htCur = htQuesOffDutyTodayResponse[taskBean.wechatMsgBean.roomid] as Hashtable;
                    if (!htCur.ContainsKey(shortDateKey))
                    {
                        canRespose = false;
                        htCur[shortDateKey] = dutyKey;
                    }
                    else
                    {
                        htCur[shortDateKey] = htCur[shortDateKey].ToString() + " " + dutyKey;
                        string oriStr = htCur[shortDateKey].ToString();
                        if(!oriStr.Contains(dutyKey))
                        {
                            canRespose = false;
                        }
                        else
                        {
                            int bingoTime = 0;
                            string[] arr = oriStr.Split(' ');
                            foreach(string v in arr)
                            {
                                if(dutyKey==v)
                                {
                                    bingoTime++;
                                }
                            }
                            if(bingoTime==5)
                            {
                                canRespose = true;
                            }
                        }
                    }
                }

                if(!canRespose)
                {
                    return;
                }
                else
                {
                    if (isOffDutyTime)
                    {
                        ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("亲亲辛苦啦，麻烦提供相关的订单信息和问题描述，喵喵会尽快为您处理!");
                        sb.AppendLine("");
                        sb.AppendLine("单号+问题描述");
                        sb.AppendLine("例如：");
                        sb.AppendLine("单号+反审/拦截（订单的反审或者拦截发货）");
                        sb.AppendLine("快递单号+催件");
                        sb.AppendLine("快递单号+改地址（新地址+姓名电话）");
                        sb.AppendLine("快递单号+签收未收到（+消费者的聊天截屏）");
                        sb.AppendLine("快递单号+登记退回（快递运输途中需要退回）");
                        sb.AppendLine("");
                        sb.AppendLine("其他问题我的小伙伴们稍晚会跟进解决，感谢亲亲的耐心等待和理解~");
                        replayMsg = sb.ToString();
                        //htHourQuery[hour.ToString()] = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd") + " " + System.DateTime.Now.Hour + ":" + "59:59");
                        ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                    }
                    return;
                }

                if (isOffDutyTime)
                {
                    if (canRespose)
                    {
                        ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                    }
                }
                else
                {
                    if (!htDayUserReplyFrequency.ContainsKey(shortDateKey))
                    {
                        htDayUserReplyFrequency.Clear();
                        htDayUserReplyFrequency[shortDateKey] = new Hashtable();
                    }

                    Hashtable htTodayReply = htDayUserReplyFrequency[shortDateKey] as Hashtable;

                    if (!htTodayReply.ContainsKey(taskBean.wechatMsgBean.from))
                    {
                        htTodayReply[taskBean.wechatMsgBean.from] = new Hashtable();
                    }

                    Hashtable htHourQuery = htTodayReply[taskBean.wechatMsgBean.from] as Hashtable;
                    int hour = System.DateTime.Now.Hour;
                    if (!htHourQuery.ContainsKey(hour.ToString()))
                    {
                        htHourQuery[hour.ToString()] = System.DateTime.Now;

                        if (!isOffDutyTime)
                        {                           
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("亲亲辛苦啦，麻烦提供相关的订单信息和问题描述，喵喵会尽快为您处理!");
                            sb.AppendLine("");
                            sb.AppendLine("单号+问题描述");  
                            sb.AppendLine("例如：");
                            sb.AppendLine("单号+反审/拦截（订单的反审或者拦截发货）");
                            sb.AppendLine("快递单号+催件");
                            sb.AppendLine("快递单号+改地址（新地址+姓名电话）");
                            sb.AppendLine("快递单号+签收未收到（+消费者的聊天截屏）");
                            sb.AppendLine("快递单号+登记退回（快递运输途中需要退回）");
                            sb.AppendLine("");
                            sb.AppendLine("其他问题我的小伙伴们稍晚会跟进解决，感谢亲亲的耐心等待和理解~");
                            replayMsg = sb.ToString();
                            htHourQuery[hour.ToString()] = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd") + " " + System.DateTime.Now.Hour + ":" + "59:59");
                            ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");

                        }
                    }
                    else
                    {
                        DateTime dtLast = Convert.ToDateTime(htHourQuery[hour.ToString()]);
                        TimeSpan ts = System.DateTime.Now - dtLast;
                        if (ts.TotalMinutes>0&& ts.TotalMinutes <= 10)
                        {
                            if (!isOffDutyTime)
                            {
                                //replayMsg = "稍等亲亲，待会同事看下。有时候消息过多回复不及时，请见谅。";
                                //replayMsg = "非常抱歉亲亲，因值班人手有限，期间若回复不及时敬请谅解！客服在线时间为08:30-22:30，群消息都会陆续处理的哈，感谢亲的耐心等待和配合~";
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine("亲亲辛苦啦，麻烦提供相关的订单信息和问题描述，喵喵会尽快为您处理!");
                                sb.AppendLine("");
                                sb.AppendLine("单号+问题描述");
                                sb.AppendLine("例如：");
                                sb.AppendLine("单号+反审/拦截（订单的反审或者拦截发货）");
                                sb.AppendLine("快递单号+催件");
                                sb.AppendLine("快递单号+改地址（新地址+姓名电话）");
                                sb.AppendLine("快递单号+签收未收到（+消费者的聊天截屏）");
                                sb.AppendLine("快递单号+登记退回（快递运输途中需要退回）");
                                sb.AppendLine("");
                                sb.AppendLine("其他问题我的小伙伴们稍晚会跟进解决，感谢亲亲的耐心等待和理解~");
                                replayMsg = sb.ToString();
                                htHourQuery[hour.ToString()] = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd") + " " + System.DateTime.Now.Hour + ":" + "59:59");
                                ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");

                            }
                        }
                    }
                    /*if (!htRoomQuesLastTriggerCheck.ContainsKey(taskBean.wechatMsgBean.roomid))
                    {
                        htRoomQuesLastTriggerCheck[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                    }
                    else
                    {
                        DateTime dtLastTriggerDraw = Convert.ToDateTime(htRoomQuesLastTriggerCheck[taskBean.wechatMsgBean.roomid]);
                        TimeSpan ts = System.DateTime.Now - dtLastTriggerDraw;
                        if (ts.TotalHours < 1)
                        {
                            return;
                        }
                    }

                    ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                    htRoomQuesLastTriggerCheck[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;*/
                }


            }
            catch (Exception ex)
            {

            }
        }

        private void handleThanks(TaskBean taskBean)
        {
            try
            {
                return;
                //暂时去掉感谢，防止哪天又要加上，直接return
                string replayMsg = "不客气亲亲，很高兴可以帮到您~";

                if (!htRoomQuesLastTriggerThank.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    htRoomQuesLastTriggerThank[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                }
                else
                {
                    DateTime dtLastTriggerThanks = Convert.ToDateTime(htRoomQuesLastTriggerThank[taskBean.wechatMsgBean.roomid]);
                    TimeSpan ts = System.DateTime.Now - dtLastTriggerThanks;
                    if (ts.TotalHours < 1)
                    {
                        return;
                    }
                }


                
                ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                htRoomQuesLastTriggerThank[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
            }
            catch(Exception ex)
            {

            }
        }

        private void handleCheck(TaskBean taskBean)
        {
            try
            {
                string replayMsg = "好的亲，待会同事处理哈~ ";
                DateTime dtNow = System.DateTime.Now;
                bool isOffDutyTime = false;
                if(dtNow.Hour>=23|| dtNow.Hour<=7)
                {
                    replayMsg = " 亲亲辛苦啦，客服伙伴们休息中哦，上班给亲处理哈~  亲也可自行操作：勾选订单 -> 多鸿下方 “更多操作”->“推送并预发货”。然后刷新就能看到单号了~";
                    isOffDutyTime = true;
                }
                if( dtNow.Hour==22)
                {
                    if(dtNow.Minute>30)
                    {
                        replayMsg = "亲亲辛苦啦，客服伙伴们休息中哦，上班给亲处理哈~  亲也可自行操作：勾选订单 -> 多鸿下方 “更多操作”->“推送并预发货”。然后刷新就能看到单号了~";
                        isOffDutyTime = true;
                    }
                }
                if (dtNow.Hour == 8)
                {
                    if (dtNow.Minute < 30)
                    {
                        replayMsg = "亲亲辛苦啦，客服伙伴们休息中哦，上班给亲处理哈~  亲也可自行操作：勾选订单 -> 多鸿下方 “更多操作”->“推送并预发货”。然后刷新就能看到单号了~";
                        isOffDutyTime = true;
                    }
                }
                string shortDateKey = System.DateTime.Now.ToShortDateString();
                bool canRespose = false;
                if(!htOffDutyTodayResponse.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    canRespose = true;
                    Hashtable htCurRoom = new Hashtable();
                    htCurRoom[shortDateKey] = "";
                    htOffDutyTodayResponse[taskBean.wechatMsgBean.roomid] = htCurRoom;

                }
                else
                {
                    Hashtable htCur = htOffDutyTodayResponse[taskBean.wechatMsgBean.roomid] as Hashtable;
                    if(!htCur.ContainsKey(shortDateKey))
                    {
                        canRespose = true;
                        htCur[shortDateKey] = "";
                    }
                }
                if(isOffDutyTime)
                {
                    if (canRespose)
                    {
                        ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                    }
                }
                else
                {
                    if (!htRoomLastTriggerCheck.ContainsKey(taskBean.wechatMsgBean.roomid))
                    {
                        htRoomLastTriggerCheck[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                    }
                    else
                    {
                        DateTime dtLastTriggerDraw = Convert.ToDateTime(htRoomLastTriggerCheck[taskBean.wechatMsgBean.roomid]);
                        TimeSpan ts = System.DateTime.Now - dtLastTriggerDraw;
                        if (ts.TotalHours < 1)
                        {
                            return;
                        }
                    }

                    ReplyHelper.Instance.replySuperMsg(taskBean.wechatMsgBean.roomid, replayMsg, "1", "");
                    htRoomLastTriggerCheck[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;

                }
                
                
            }
            catch(Exception ex)
            {

            }
        }

        private void handleGenDan(TaskBean taskBean)
        {
            try
            {
                WechatMsgBean wechatMsgBean = taskBean.wechatMsgBean;
                if (htGendanRooms.ContainsKey(taskBean.expressCus))
                {
                    string roomid = htGendanRooms[taskBean.expressCus].ToString();


                    ArrayList arListSto = new ArrayList();
                    ArrayList arListJt = new ArrayList();
                    ArrayList arListZto = new ArrayList();
                    ArrayList arListYto = new ArrayList();
                    ArrayList arListEms = new ArrayList();

                    Hashtable htCusomer = new Hashtable();
                    bool res = true;
                    bool isContainDouyin = false;


                    if (!UtilityHelp.Instance.isContainMoreCustomers(wechatMsgBean.content, arListJt, arListSto, arListZto, arListEms, arListYto,htCusomer, out isContainDouyin))
                    {
                        ReplyHelper.Instance.replySuperMsg(roomid, wechatMsgBean.content, "1", "");
                        WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                        newWechatMsgBean.roomid = roomid;
                        newWechatMsgBean.content = wechatMsgBean.content;
                        newWechatMsgBean.dtSendTime = System.DateTime.Now;
                        htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                    }

                    else
                    {
                        foreach (DictionaryEntry de in htCusomer)
                        {
                            string customer = de.Key.ToString();
                            roomid = htGendanRooms[customer].ToString();
                            string leftMessage = "";
                            if (customer.Contains("STO"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListJt, arListZto, arListEms);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListJt, arListZto, arListEms,arListYto);
                                leftMessage.Trim('\n');
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                                System.Threading.Thread.Sleep(50);
                            }
                            if (customer.Contains("ZTO"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListJt, arListSto, arListEms);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListJt, arListSto, arListEms,arListYto);
                                
                                leftMessage.Trim('\n');
                               
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");
                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                                System.Threading.Thread.Sleep(50);
                            }
                            if (customer.Contains("JT"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListSto, arListZto, arListEms);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListSto, arListZto, arListEms, arListYto);
                                leftMessage.Trim('\n');


                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                                System.Threading.Thread.Sleep(50);
                            }
                            if (customer.Contains("EMS"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListSto, arListZto, arListJt);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListSto, arListZto, arListJt, arListYto);
                                leftMessage.Trim('\n');
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                                System.Threading.Thread.Sleep(50);
                            }


                            if (customer.Contains("YT"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListSto, arListZto, arListJt);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListSto, arListZto, arListJt, arListEms);
                                leftMessage.Trim('\n');
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                                System.Threading.Thread.Sleep(50);
                            }

                        }
                    }

                    if (res)
                    {
                        string replayMsg = UtilityHelp.Instance.getResponseMsg(wechatMsgBean.content, taskBean.taskType);
                        

                        if (taskBean.replyCustomer)
                        {
                            ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, replayMsg, "1", "");
                        }
                            

                    }


                }
            }
            catch (Exception ex)
            {

            }
        }

        private void handleLanshou(TaskBean taskBean)
        {
            try
            {
                WechatMsgBean wechatMsgBean = taskBean.wechatMsgBean;
                if (htLanshopuRooms.ContainsKey(taskBean.expressCus))
                {
                    string roomid = htLanshopuRooms[taskBean.expressCus].ToString();

                    ArrayList arListSto = new ArrayList();
                    ArrayList arListJt = new ArrayList();
                    ArrayList arListZto = new ArrayList();
                    ArrayList arListEms = new ArrayList();

                    ArrayList arListYto = new ArrayList();

                    Hashtable htCusomer = new Hashtable();
                    bool res = true;
                    bool isContainDouyin = false;
                    if (!UtilityHelp.Instance.isContainMoreCustomers(wechatMsgBean.content, arListJt, arListSto, arListZto, arListEms, arListYto, htCusomer,out isContainDouyin))
                    {
                        wechatMsgBean.content = wechatMsgBean.content.Replace("、", " ");
                        ReplyHelper.Instance.replySuperMsg(roomid, wechatMsgBean.content, "1", "");

                        WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                        newWechatMsgBean.roomid = roomid;
                        newWechatMsgBean.content = wechatMsgBean.content;
                        newWechatMsgBean.dtSendTime = System.DateTime.Now;
                        htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                    }
                    else
                    {
                        foreach(DictionaryEntry de in htCusomer)
                        {
                            string customer = de.Key.ToString();
                            roomid = htLanshopuRooms[customer].ToString();
                            string leftMessage = "";
                            if (customer.Contains("STO"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListJt, arListZto, arListEms);

                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListJt, arListZto, arListEms, arListYto);

                                leftMessage = leftMessage.Trim('\n');
                                leftMessage = leftMessage.Replace("、", " ");
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                                System.Threading.Thread.Sleep(50);
                            }
                            if (customer.Contains("ZTO"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListJt, arListSto, arListEms);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListJt, arListSto, arListEms, arListYto);
                                leftMessage =leftMessage.Trim('\n');
                                leftMessage = leftMessage.TrimStart('\n');
                                leftMessage = leftMessage.Replace("、", " ");
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                                System.Threading.Thread.Sleep(50);
                            }
                            if (customer.Contains("JT"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListSto, arListZto, arListEms);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListSto, arListZto, arListEms, arListYto);
                                leftMessage = leftMessage.Trim('\n');
                                leftMessage = leftMessage.Replace("、", " ");
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");
                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;

                                System.Threading.Thread.Sleep(50);
                            }

                            if (customer.Contains("EMS"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListSto, arListZto, arListJt);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListSto, arListZto, arListJt, arListYto);
                                leftMessage = leftMessage.Trim('\n');
                                leftMessage = leftMessage.Replace("、", " ");
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                                System.Threading.Thread.Sleep(50);
                            }

                            if (customer.Contains("YT"))
                            {
                                //leftMessage = UtilityHelp.Instance.getLeftMessage(wechatMsgBean.content, arListSto, arListZto, arListJt);
                                leftMessage = UtilityHelp.Instance.getLeftMessageWithYto(wechatMsgBean.content, arListSto, arListZto, arListJt, arListEms);
                                leftMessage = leftMessage.Trim('\n');
                                leftMessage = leftMessage.Replace("、", " ");
                                ReplyHelper.Instance.replySuperMsg(roomid, leftMessage, "1", "");

                                WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                                newWechatMsgBean.roomid = roomid;
                                newWechatMsgBean.content = leftMessage;
                                newWechatMsgBean.dtSendTime = System.DateTime.Now;
                                htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                                System.Threading.Thread.Sleep(50);
                            }

                        }
                    }
                   
                    if(res)
                    {
                        string replayMsg = UtilityHelp.Instance.getResponseMsg(wechatMsgBean.content, taskBean.taskType);
                        /*if(isContainDouyin&& wechatMsgBean.content.Contains("揽"))
                        {
                            if(!wechatMsgBean.roomid.Contains("10839512734750675")&& !wechatMsgBean.roomid.Contains("10910190745564860"))
                            {
                                replayMsg = replayMsg + "。" + "但是现在抖音平台与快递公司在考核，不一定能预揽收上，我们先通知快递处理，带来不便实在抱歉了亲亲 QAQ";
                            }
                            
                        }*/
                        if(wechatMsgBean.content.Contains("揽")&& !wechatMsgBean.roomid.Contains("10839512734750675") && !wechatMsgBean.roomid.Contains("10910190745564860"))
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine(replayMsg);
                            //sb.AppendLine("因个别平台考核可能无法预揽，我们先催促快递公司处理。");
                            //sb.AppendLine("带来不便实在抱歉了亲亲 QAQ");
                            replayMsg = sb.ToString();

                            //replayMsg = replayMsg + "。" + "揽收已通知~因个别平台考核可能无法预揽，我们先催促快递公司处理。带来不便实在抱歉了亲亲 QAQ";
                        }
                        ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, replayMsg, "1", "");

                    }


                }
            }
            catch (Exception ex)
            {

            }
        }


        private void handleCustomNotice(TaskBean taskBean)
        {
            try
            {

                if (!htRoomLastTriggerCustomService.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    htRoomLastTriggerCustomService[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                }
                else
                {
                    DateTime dtLastTriggerDraw = Convert.ToDateTime(htRoomLastTriggerCustomService[taskBean.wechatMsgBean.roomid]);
                    TimeSpan ts = System.DateTime.Now - dtLastTriggerDraw;
                    if (ts.TotalMinutes < 1)
                    {
                        return;
                    }
                }


                WechatMsgBean wechatMsgBean = taskBean.wechatMsgBean;
                StringBuilder sb = new StringBuilder();
                foreach (DictionaryEntry de in htCustomerServiceRoomsUsers)
                {
                    sb.Append(de.Key.ToString() + ",");
                }
         
                if (sb.Length > 1)
                {
                    ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, "", "1", sb.ToString().TrimEnd(','));
                    htRoomLastTriggerCustomService[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void handleDrawing(TaskBean taskBean)
        {
            try
            {
           
                if(!htRoomLastTriggerDraw.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    htRoomLastTriggerDraw[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                }
                else
                {
                    DateTime dtLastTriggerDraw = Convert.ToDateTime(htRoomLastTriggerDraw[taskBean.wechatMsgBean.roomid]);
                    TimeSpan ts = System.DateTime.Now - dtLastTriggerDraw;
                    if (ts.TotalMinutes < 1)
                    {
                        return;
                    }
                }
                

                WechatMsgBean wechatMsgBean = taskBean.wechatMsgBean;
                if(htDrawingRoomsUsers.ContainsKey(wechatMsgBean.roomid))
                {
                    ArrayList arListAtList = htDrawingRoomsUsers[wechatMsgBean.roomid] as ArrayList;
                    StringBuilder sb = new StringBuilder();
                    foreach(string wxid in arListAtList)
                    {
                        sb.Append(wxid + ",");
                    }
                    if(sb.Length>1)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, "", "1", sb.ToString().TrimEnd(','));
                        htRoomLastTriggerDraw[taskBean.wechatMsgBean.roomid]=System.DateTime.Now;

                        WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                        newWechatMsgBean.roomid = wechatMsgBean.roomid;
                        newWechatMsgBean.content = "";
                        newWechatMsgBean.dtSendTime = System.DateTime.Now;
                        newWechatMsgBean.atUserListString = sb.ToString().TrimEnd(',');
                        //htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                        htAtRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                    }
                    

                }
            }
            catch(Exception ex)
            {

            }
        }

        private void handleCoding(TaskBean taskBean)
        {
            try
            {
                if (!htRoomLastTriggerCoding.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                }
                else
                {
                    DateTime dtLastTriggerCoding = Convert.ToDateTime(htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid]);
                    TimeSpan ts = System.DateTime.Now - dtLastTriggerCoding;
                    if (ts.TotalMinutes < 1)
                    {
                        return;
                    }
                }

                bool isFestivaTime = StoParams.Instance.isFestivalTime;
                WechatMsgBean wechatMsgBean = taskBean.wechatMsgBean;
                if (isFestivaTime)
                {
                    if(hVipRooms.ContainsKey(wechatMsgBean.roomid.ToString()))
                    {
                        isFestivaTime = false;
                    }
                }



               
                if (htCodingRoomsUsers.ContainsKey(wechatMsgBean.roomid))
                {
                    ArrayList arListAtList = htCodingRoomsUsers[wechatMsgBean.roomid] as ArrayList;
                    StringBuilder sb = new StringBuilder();
                    foreach (string wxid in arListAtList)
                    {
                        sb.Append(wxid + ",");
                    }
                    string responceContent = "";
                    if(isFestivaTime)
                    {
                        responceContent = "（亲亲抱歉，已暂停编码制作服务，预计2 月 13 日恢复。期间请勿改动后台链接，以免影响推单发货 。）";

                        ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, responceContent, "1",  sb.ToString().TrimEnd(','));
                        htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
          

                    }
                    else
                    {
                        if (sb.Length > 1)
                        {
                            ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, "", "1", sb.ToString().TrimEnd(','));
                            htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                            WechatMsgBean newWechatMsgBean = new WechatMsgBean();
                            newWechatMsgBean.roomid = wechatMsgBean.roomid;
                            newWechatMsgBean.content = "";
                            newWechatMsgBean.dtSendTime = System.DateTime.Now;
                            newWechatMsgBean.atUserListString = sb.ToString().TrimEnd(',');
                            htAtRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                            //htRetryMsg[System.DateTime.Now.ToString()] = newWechatMsgBean;
                        }
                    }


                   


                }
            }
            catch (Exception ex)
            {

            }
        }


        private void handleChangeCoding(TaskBean taskBean)
        {
            try
            {
                if (!htRoomLastTriggerCoding.ContainsKey(taskBean.wechatMsgBean.roomid))
                {
                    htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                }
                else
                {
                    DateTime dtLastTriggerCoding = Convert.ToDateTime(htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid]);
                    TimeSpan ts = System.DateTime.Now - dtLastTriggerCoding;
                    if (ts.TotalMinutes < 1)
                    {
                        return;
                    }
                }
               

                WechatMsgBean wechatMsgBean = taskBean.wechatMsgBean;

                bool isFestivaTime = StoParams.Instance.isFestivalTime;
                if (isFestivaTime)
                {
                    if (hVipRooms.ContainsKey(wechatMsgBean.roomid.ToString()))
                    {
                        isFestivaTime = false;
                    }
                }

                if (htCodingRoomsUsers.ContainsKey(wechatMsgBean.roomid))
                {
                    ArrayList arListAtList = htCodingRoomsUsers[wechatMsgBean.roomid] as ArrayList;
                    StringBuilder sb = new StringBuilder();
                    foreach (string wxid in arListAtList)
                    {
                        sb.Append(wxid + ",");
                    }
                    string responceContent = "";
                    if (isFestivaTime)
                    {
                        responceContent = "（亲亲抱歉，已暂停编码制作服务，预计2 月 13 日恢复。期间请勿改动后台链接，以免影响推单发货 。）";

                        ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, responceContent, "1", sb.ToString().TrimEnd(','));
                        htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;

                    }

                    else if (sb.Length > 1)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, "", "1", sb.ToString().TrimEnd(','));
                        htRoomLastTriggerCoding[taskBean.wechatMsgBean.roomid] = System.DateTime.Now;
                    }


                }
            }
            catch (Exception ex)
            {

            }
        }


        public void handleStatics(WechatMsgBean wechatMsgBean)
        {
            try
            {
                string outPutPath = "";
                bool res = UtilityHelp.Instance.genStaticExcel(out outPutPath, wechatMsgBean.roomid, wechatMsgBean.content, wechatMsgBean.roomid);
                string response = "";
                if (res)
                {
                    response = "生成统计报表成功\r\n" + outPutPath;
                }
                else
                {
                    response = "生成统计报表失败";
                }

                ReplyHelper.Instance.replySuperMsg(wechatMsgBean.roomid, response);
     
            }
            catch (Exception ex)
            {

            }
        }


        public void handleOutProblemAck(WechatMsgBean wechatMsgBean)
        {
            try
            {
                OutboundProblemAckGen.Instance.BeginAnalyThread("", "", wechatMsgBean.roomid, wechatMsgBean.from);              
            }
            catch (Exception ex)
            {

            }
        }


        public void handleStatics(GroupPos curGroupPos, string strLine)
        {
            try
            {
                string outPutPath = "";
                bool res=UtilityHelp.Instance.genStaticExcel(out outPutPath, curGroupPos.groupName, strLine);
                string response = "";
                if (res)
                {
                    response = "生成统计报表成功\r\n" + outPutPath;                   
                }
                else
                {
                    response = "生成统计报表失败" ;
                }
                //byte[] resMsg = System.Text.Encoding.Default.GetBytes(response);
                htReplyCuePos[curGroupPos] = response;
                //ReplyHelper.Instance.replyMsg(response, curGroupPos);
                //Program.weServer.sendContent(resMsg);
            }
            catch(Exception ex)
            {

            }
        }

        private void handleDupResponse(ArrayList arListDupParcel)
        {
            try
            {
                foreach(ParcelBean parcelBean in arListDupParcel)
                {
                    if(!htDupResponseTimes.ContainsKey(parcelBean.dupTlicateType+" "+ parcelBean.wayBill))
                    {
                        htDupResponseTimes[parcelBean.dupTlicateType + " " + parcelBean.wayBill] = 1;
                    }
                    else
                    {
                        htDupResponseTimes[parcelBean.dupTlicateType + " " + parcelBean.wayBill] = Convert.ToInt32(htDupResponseTimes[parcelBean.dupTlicateType + " " + parcelBean.wayBill].ToString()) + 1;
                    }
                    int curDupTimes = Convert.ToInt32(htDupResponseTimes[parcelBean.dupTlicateType + " " + parcelBean.wayBill].ToString());
                    if(curDupTimes>3)
                    {
                        return;
                    }
                    StoParams.Instance.htResWechat[parcelBean.wayBill] = parcelBean.dupTlicateType + " 重复任务";
                }
                arListDupParcel.Clear();

            }
            catch(Exception ex)
            {

            }
        }

        public void initLog()
        {
            string dir = System.Environment.CurrentDirectory;
            bFile = new FileStream(dir + "\\error.txt", FileMode.Append);
        }
        public void DecodeMessageFrame(Object msgObj)
        {
            try
            {
                string res = "ack";
                FrameMessage frameMessage = msgObj as FrameMessage;
                string strContent = frameMessage.Content;
                string msg = "";
                AppSession session = frameMessage.Session;
                if (strContent.Length > 12)
                {
                    //parseTcpWechat(strContent,out msg);
                    WechatMsgBean wechatMsgBean = new WechatMsgBean();
                    parseCompanyTcpWechat(strContent, out msg, wechatMsgBean);
                }
              


            }
            catch (Exception e)
            {
                //SimbaCameraScale.Common.Log.Logger.LogError(null, e.Message);
            }
        }

        public bool isTime(string strContent)
        {
            bool res = false;
            try
            {

                DateTime dt = Convert.ToDateTime(strContent);
                res = true;
            }
            catch (Exception ex)
            {

            }
            return res;

        }

        public bool initQueRecentTask()
        {
            bool res = false;
            try
            {
                ArrayList recentMsgTask = Transaction.TransactionDatabase.Instance.getRecentMsg(out res);
                foreach(string strTask in recentMsgTask)
                {
                   
                    taskQue.Enqueue(strTask);
                }
            }
            catch(Exception ex)
            {

            }
            return res;
        }

        public void resetFilter()
        {
            try
            {
                arListTaskFilters.Clear();
                taskTodoQue.Clear();
            }
            catch(Exception ex)
            {

            }
        }
        public void removeOverTimeParcel()
        {
            try
            {

                if(arListTaskFilters.Count>3)
                {
                    int totalArCount = arListTaskFilters.Count;
                    if(totalArCount>5)
                    {
                        LogHelper.Instance.writeLog("total count=" + totalArCount.ToString());
                        arListTaskFilters.RemoveAt(0);
                        //arListTaskFilters.RemoveAt(0);
                    }
                    else
                    {
                        arListTaskFilters.RemoveAt(0);
                    }
                    ArrayList arListTodel = new ArrayList();
                    Hashtable htMap = new Hashtable();
                    foreach(ParcelBean parcelBean in arListTaskFilters)
                    {
                        TimeSpan ts = System.DateTime.Now - parcelBean.dtToDoTaskTime;
                        if(ts.TotalSeconds>30)
                        {
                            arListTodel.Add(parcelBean);
                            htMap[parcelBean.wayBill] = parcelBean;
                        }
                    }
                    int removeCount = 0; 
                    foreach(ParcelBean parcel in arListTodel)
                    {
                        if(htMap.ContainsKey(parcel.wayBill))
                        {
                            ParcelBean parcelT = htMap[parcel.wayBill] as ParcelBean;
                            if (arListTaskFilters.Contains(parcelT))
                            {
                                arListTaskFilters.Remove(parcelT);
                                removeCount++;
                            }
                        }
                       
                    }

                    LogHelper.Instance.writeLog(" remove total count=" + removeCount.ToString());
                }
                int totalCount = taskQue.Count;
                int toRemoveCount = totalCount - 5;
                if(toRemoveCount>0)
                {
                    while(toRemoveCount>0)
                    {
                        taskQue.Dequeue();
                        toRemoveCount--;
                    }
                }

                ArrayList arListToDel = new ArrayList();
                foreach(DictionaryEntry de in htHandleGenRecords)
                {
                    DateTime dtS = Convert.ToDateTime(de.Value);
                    TimeSpan tts = DateTime.Now - dtS;
                    if(tts.TotalMinutes>5)
                    {
                        arListToDel.Add(de.Key.ToString());
                    }
                }
                foreach(string line in arListToDel)
                {
                    if(htHandleGenRecords.ContainsKey(line))
                    {
                        htHandleGenRecords.Remove(line);
                    }
                }
                

            }
            catch(Exception ex)
            {

            }
        }


        public void parseAckTcpWechat(string content, out string lastTask, WechatMsgBean wechatBean)
        {
            lastTask = "";
            try
            {
                //if(wechatBean.from==StoParams.Instance.replyWechat)
                {
                    //LogHelper.Instance.writeLog("收到ack消息 roomid="+ wechatBean.roomid+" content="+ wechatBean.content);
                    TaskBean taskBean = new TaskBean();
                    UtilityHelp.Instance.removeRetryMsg(wechatBean, taskBean);
                }
                
            }
            catch(Exception ex)
            {
                LogHelper.Instance.writeLog("parseAckTcpWechat ex=" + ex.ToString());
            }
        }


        //实时处理tcp消息
        public string parseCompanyTcpWechat(string content, out string lastTask,WechatMsgBean wechatBean)
        {
            lastTask = "";
            try
            {
                //LogHelper.Instance.writeLog("recv tcp tmp " + wechatBean.content);
                if (wechatBean.from == StoParams.Instance.replyWechat)
                {
                    if(wechatBean.isAtMsg)
                    {
                        handleAtMsgAck(wechatBean);
                    }
                    return "";
                }
                /*if(lastHandleTcpMessage== wechatBean.content)
                {
                    return "";
                }*/
                lastHandleTcpMessage = wechatBean.content;
                LogHelper.Instance.writeLog("recv tcp "+wechatBean.content);
                ArrayList arTaskDone = new ArrayList();
                removeOverTimeParcel();
                content = content.Replace("栏截", "拦截");
                content = content.Replace("极兔速递一联单", "");
                content = content.Replace("极兔速递,JT", "JT");
                content = content.Replace("极兔速递 运单号：JT", "JT");


                wechatBean.content = wechatBean.content.Replace("栏截", "拦截");
                wechatBean.content = wechatBean.content.Replace("极兔速递一联单", "");
                wechatBean.content = wechatBean.content.Replace("极兔速递, JT", "JT");
                wechatBean.content = wechatBean.content.Replace("极兔速递,JT", "JT");
                wechatBean.content = wechatBean.content.Replace("极兔速递，JT", "JT");
                wechatBean.content = wechatBean.content.Replace("京旗联盟-极兔 JT", "JT");
                wechatBean.content = wechatBean.content.Replace("京旗联盟-极兔，JT", "JT");
                wechatBean.content = wechatBean.content.Replace("京旗联盟-极兔,JT", "JT");

                wechatBean.content = wechatBean.content.Replace("极兔速递 运单号：JT", "JT");
                wechatBean.content = wechatBean.content.Replace("极免速递 单号: JT", "JT");
                wechatBean.content = wechatBean.content.Replace("极免速递  单号: JT", "JT");
                wechatBean.content = wechatBean.content.Replace("极兔速递 JT", "JT");
                wechatBean.content = wechatBean.content.Replace("极兔速递JT", "JT");
                wechatBean.content = wechatBean.content.Replace("运单号码：JT", "JT");
                wechatBean.content = wechatBean.content.Replace("运单号JT", "JT");
                wechatBean.content = wechatBean.content.Replace("运单号\nJT", "JT");


                wechatBean.content = UtilityHelp.Instance.removeFirstLineTime(wechatBean.content);
                //wechatBean.content = UtilityHelp.Instance.removeYTParcel(wechatBean);
                wechatBean.content = wechatBean.content.Replace("，", ",");
                wechatBean.content = wechatBean.content.Replace(",7", " 7");
                wechatBean.content = wechatBean.content.Replace(",JT", " JT");

                wechatBean.content = wechatBean.content.Replace(",YT", " YT");

                int chinseIndex = UtilityHelp.Instance.getChineseIndex(wechatBean.content);
                if(chinseIndex>0&& wechatBean.content.Length>13)
                {
                    string firstSub = wechatBean.content.Substring(0, chinseIndex);
                    string nextSub = wechatBean.content.Substring(chinseIndex);
                    firstSub = firstSub.Replace(",", " ");
                    wechatBean.content = firstSub + nextSub;
                }







                //去掉图片链接，后面也好加上
                wechatBean.content = UtilityHelp.Instance.removeQQPngLink(wechatBean.content);

                wechatBean.content = UtilityHelp.Instance.changeHDTHParcel(wechatBean.content);
                wechatBean.content = UtilityHelp.Instance.removeHeader(wechatBean.content);

                wechatBean.content = UtilityHelp.Instance.removeHeaderByReference(wechatBean.content);
                //LogHelper.Instance.writeLog("recv msg:" + wechatBean.content);

                lastTask = "";
                string outSetTokenMsg = "";

                if (FrameDecoderManager.Instance.htRoomClose.ContainsKey(wechatBean.roomid) && !wechatBean.content.Contains("开启"))
                {
                    return "";
                }
                if(UtilityHelp.Instance.isNotTask(wechatBean.content))
                {
                    return "";
                }


                ArrayList arListJT = UtilityHelp.Instance.getInputBarcodesAndAddr(wechatBean.content,new TaskBean());
                if(arListJT.Count>0)
                {
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "包含极兔非我司订单哦");
                    UtilityHelp.Instance.removeJTBarcode(wechatBean, arListJT);
                    //return "";
                }



                if (content.Contains("loginTime") && content.Contains("token"))
                {
                    UtilityHelp.Instance.setToken(content, out outSetTokenMsg);
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, outSetTokenMsg);
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, outSetTokenMsg, wechatBean.robotQQ);
                    }
                    return "";
                }

                else if (wechatBean.content.Contains("loginTime") && wechatBean.content.Contains("token"))
                {
                    UtilityHelp.Instance.setToken(wechatBean.content, out outSetTokenMsg);
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置token成功");
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "设置token成功", wechatBean.robotQQ);
                    }
                    return "";
                }

                else if (wechatBean.content.Contains("设置留言微信:") || wechatBean.content.Contains("设置留言微信："))
                {
                    LogHelper.Instance.writeLog("解析到任务设置留言微信：" + wechatBean.content);
                    string outMsg = "";
                    UtilityHelp.Instance.setUserWechat(wechatBean, out outMsg);
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置留言微信" + outMsg);
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "设置留言微信" + outMsg, wechatBean.robotQQ);
                    }

                    return "";
                }

                else if (wechatBean.content.Contains("关闭机器人"))
                {
                    LogHelper.Instance.writeLog("解析到关闭机器人 ：" + wechatBean.content);
                    UtilityHelp.Instance.setRoomClose(wechatBean, "close");
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "关闭机器人成功");
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "关闭机器人成功", wechatBean.robotQQ);
                    }

                    return "";
                }

                else if (wechatBean.content.Contains("开启机器人"))
                {
                    LogHelper.Instance.writeLog("解析到开启机器人 ：" + wechatBean.content);
                    UtilityHelp.Instance.setRoomClose(wechatBean, "open");
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "开启机器人成功");
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "开启机器人成功", wechatBean.robotQQ);
                    }

                    return "";
                }



                else if (wechatBean.content.Contains("注册网点"))
                {
                    UtilityHelp.Instance.addRoomConfigFile(wechatBean);
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "注册网点成功");
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "注册网点成功", wechatBean.robotQQ);
                    }

                    return "";
                }

                else if (wechatBean.content.Contains("兜底客服"))
                {
                    this.sendFileNotice(wechatBean.roomid, wechatBean);
                    return "";
                }


                else if (wechatBean.content.Contains("设置网点客服"))
                {
                    UtilityHelp.Instance.addRoomEmployeeConfigFile(wechatBean);
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置网点客服成功");
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "设置网点客服成功", wechatBean.robotQQ);
                    }

                    return "";
                }

                else if (wechatBean.content.Contains("设置群名称"))
                {
                    UtilityHelp.Instance.addRoomGroupConfigFile(wechatBean);
                    if (!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置群名称成功");
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(wechatBean.roomid, "设置群名称成功", wechatBean.robotQQ);
                    }

                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置快递指令"))
                {
                    string msgput = "";
                    UtilityHelp.Instance.resetCommadnGendan(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置快递指令 " + msgput);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消快递指令"))
                {
                    string msgput = "";
                    UtilityHelp.Instance.removeCommadnGendan(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消快递指令 " + msgput);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置艾特美工指令"))
                {

                    string msgput = "";
                    UtilityHelp.Instance.resetDrawCommad(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置艾特美工指令 " + msgput);
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消艾特美工指令"))
                {

                    string msgput = "";
                    UtilityHelp.Instance.removeCommadDraw(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消艾特美工指令 " + msgput);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置做编码指令"))
                {

                    string msgput = "";
                    UtilityHelp.Instance.resetCodingCommad(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置做编码指令 " + msgput);
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消做编码指令"))
                {

                    string msgput = "";
                    UtilityHelp.Instance.removeCommadCoding(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消做编码指令 " + msgput);
                    return "";
                }



                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置改编码指令"))
                {

                    string msgput = "";
                    UtilityHelp.Instance.resetChangeCodingCommad(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置改编码指令 " + msgput);
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消改编码指令"))
                {

                    string msgput = "";
                    UtilityHelp.Instance.removeChangeCommadCoding(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消改编码指令 " + msgput);
                    return "";
                }






                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消设置美工"))
                {
                    UtilityHelp.Instance.resetRoomDrawingWechat(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消设置美工用户成功");
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消设置编码"))
                {
                    UtilityHelp.Instance.resetRoomCodingWechat(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消设置编码用户成功");
                    return "";
                }


                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置疑问指令"))
                {
                    string msgput = "";
                    UtilityHelp.Instance.resetCommadnQuestion(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置疑问指令 " + msgput);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消疑问指令"))
                {
                    string msgput = "";
                    UtilityHelp.Instance.removeCommadQuestion(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消疑问指令 " + msgput);
                    return "";
                }


                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置审单指令"))
                {
                    string msgput = "";
                    UtilityHelp.Instance.resetCommadnCheck(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置审单指令 " + msgput);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("取消审单指令"))
                {
                    string msgput = "";
                    UtilityHelp.Instance.removeCommadCheck(wechatBean, out msgput);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "取消审单指令 " + msgput);
                    return "";
                }




                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("显示快递指令"))
                {
                    string allContent = UtilityHelp.Instance.getAllGendanCommand(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, allContent);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("显示美工指令"))
                {
                    string allContent = UtilityHelp.Instance.getAllDrawCommand(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, allContent);
                    return "";
                }

                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("显示做编码指令"))
                {
                    string allContent = UtilityHelp.Instance.getAllCodingCommand(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, allContent);
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("显示改编码指令"))
                {
                    string allContent = UtilityHelp.Instance.getAllChangeCodingCommand(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, allContent);
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("显示疑问指令"))
                {
                    string allContent = UtilityHelp.Instance.getAllQuestionCommand(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, allContent);
                    return "";
                }
                else if (wechatBean.content.Trim().Replace("@", "").StartsWith("显示审单指令"))
                {
                    string allContent = UtilityHelp.Instance.getAllCheckCommand(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, allContent);
                    return "";
                }


                else if (wechatBean.content.Contains("@"))
                {
                    if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置美工"))
                    {
                        if (wechatBean.atUserList.Count > 0)
                        {
                            UtilityHelp.Instance.setRoomDrawingWechat(wechatBean);
                            ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置美工用户成功");
                        }
                        return "";
                    }

                    if (wechatBean.content.Trim().Replace("@", "").StartsWith("批量设置美工"))
                    {
                        if (wechatBean.atUserList.Count > 0)
                        {
                            UtilityHelp.Instance.batchSetRoomDrawingWechat(wechatBean);
                            ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "批量设置美工用户成功");
                        }
                        return "";
                    }


                    if (wechatBean.content.Trim().Replace("@", "").StartsWith("批量设置客服"))
                    {
                        if (wechatBean.atUserList.Count > 0)
                        {
                            UtilityHelp.Instance.batchSetCustomerEmpWechat(wechatBean);
                            ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "批量设置客服成功");
                        }
                        return "";
                    }


                    if (wechatBean.content.Trim().Replace("@", "").StartsWith("批量设置编码"))
                    {
                        if (wechatBean.atUserList.Count > 0)
                        {
                            UtilityHelp.Instance.batchSetRoomCodingWechat(wechatBean);
                            ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "批量设置编码用户成功");
                        }
                        return "";
                    }




                    if (wechatBean.content.Trim().Replace("@", "").StartsWith("设置编码"))
                    {
                        if (wechatBean.atUserList.Count > 0)
                        {
                            UtilityHelp.Instance.setRoomCodingWechat(wechatBean);
                            ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "设置编码用户成功");
                        }
                        return "";
                    }


                    if (wechatBean.content.Trim().Replace("@", "").StartsWith("屏蔽快递用户"))
                    {
                        if (wechatBean.atUserList.Count > 0)
                        {
                            UtilityHelp.Instance.setInternalWechat(wechatBean);
                            ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "屏蔽快递用户成功");
                        }
                        return "";
                    }

                }






                else if (wechatBean.content.Trim().StartsWith("屏蔽我") || wechatBean.content.Trim().StartsWith("请屏蔽我"))
                {
                    UtilityHelp.Instance.setRoomFilterWechat(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "屏蔽用户成功");
                    return "";
                }

                else if (wechatBean.content.Trim().StartsWith("关闭@"))
                {
                    htRoomATClose[wechatBean.roomid] = "";
                    return "";
                }
                else if (wechatBean.content.Trim().StartsWith("开启@"))
                {
                    if (htRoomATClose.ContainsKey(wechatBean.roomid))
                    {
                        htRoomATClose.Remove(wechatBean.roomid);
                    }
                    return "";
                }


                else if (wechatBean.content.Trim().StartsWith("开启我") || wechatBean.content.Trim().StartsWith("请开启我"))
                {
                    UtilityHelp.Instance.setRoomFilterRemoveWechat(wechatBean);
                    ReplyHelper.Instance.replySuperMsg(wechatBean.roomid, "开启用户成功");
                    return "";
                }



                if (wechatBean.content.Contains("统计跟单") || wechatBean.content.Contains("跟单统计"))
                {
                    handleStatics(wechatBean);
                }

                if (wechatBean.content.EndsWith("出港问题件回复"))
                {
                    handleOutProblemAck(wechatBean);
                }


                /*if (UtilityHelp.Instance.isFilterWechat(wechatBean))
                {
                    return "";
                }*/

                ArrayList arListWechatMsg = new ArrayList();
                Hashtable htRoomUserInfo = new Hashtable();

                bean.TaskBean taskBean = new bean.TaskBean();
                UtilityHelp.Instance.getTaskBean(wechatBean, taskBean);
                LogHelper.Instance.writeLog("taskType:" + taskBean.taskType);
                if(taskBean.taskType.Contains("揽收&跟单"))
                {
                    Hashtable ht = UtilityHelp.Instance.getTaskBeanSplit(wechatBean);

                    if(ht["lanshou"].ToString().Length>10)
                    {
                        WechatMsgBean wechagLanshouBean = new WechatMsgBean();
                        wechagLanshouBean.content = ht["lanshou"].ToString();
                        bool containWaybill = false;
                        foreach(string waybill in taskBean.arListWaybills)
                        {
                            if(wechagLanshouBean.content.Contains(waybill))
                            {
                                containWaybill = true;
                            }

                        }
                        if(containWaybill)
                        {
                            wechagLanshouBean.roomid = wechatBean.roomid;
                            wechagLanshouBean.from = wechatBean.from;
                            wechagLanshouBean.isSuperWay = wechatBean.isSuperWay;
                            bean.TaskBean lanshouTaskBean = new bean.TaskBean();
                            lanshouTaskBean.wechatMsgBean = wechagLanshouBean;
                            UtilityHelp.Instance.getTaskBean(wechagLanshouBean, lanshouTaskBean);
                            taskTodoQue.Enqueue(lanshouTaskBean);
                        }
                       

                    }
                    if (ht["gendan"].ToString().Length > 10)
                    {
                        WechatMsgBean wechagLanshouBean = new WechatMsgBean();
                        wechagLanshouBean.content = ht["gendan"].ToString();
                        wechagLanshouBean.roomid = wechatBean.roomid;
                        wechagLanshouBean.from = wechatBean.from;
                        wechagLanshouBean.isSuperWay = wechatBean.isSuperWay;
                        bean.TaskBean gendanTaskBean = new bean.TaskBean();
                        gendanTaskBean.wechatMsgBean = wechagLanshouBean;
                        UtilityHelp.Instance.getTaskBean(wechagLanshouBean, gendanTaskBean);
                        taskTodoQue.Enqueue(gendanTaskBean);

                    }
                    return "";

                }

                
                if (taskBean.taskType == "")
                {
                    return "";
                }


                taskBean.wechatMsgBean = wechatBean;
                taskTodoQue.Enqueue(taskBean);
            }
            catch(Exception ex)
            {
                LogHelper.Instance.writeLog("parseCompanyTcpWechat ex" + ex.ToString() + " 行号：" + ex.StackTrace.ToString());
            }
           
            return "";
            




           
            
 
        }

        private void handleAtMsgAck(WechatMsgBean wechatBean)
        {
            try
            {
                if(wechatBean.content!="")
                {
                    return;
                }
                ArrayList arListToDel = new ArrayList();
                foreach(DictionaryEntry de in htAtRetryMsg)
                {
                    string strKey = de.Key.ToString();
                    WechatMsgBean curWechatMsgBean = (WechatMsgBean)de.Value;
                    if(curWechatMsgBean.roomid== wechatBean.roomid)
                    {
                        arListToDel.Add(strKey);
                    }
                }
                foreach(string delKey in arListToDel)
                {
                    if(htAtRetryMsg.ContainsKey(delKey))
                    {
                        htAtRetryMsg.Remove(delKey);
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }

        private void handleMsgBoard(WechatMsgBean wechatBean)
        {
            try
            {
                //GetMsgBoardHelperGen.Instance.BeginAnalyThread();
                //GetMsgBoardHelperGen.Instance.parseLeaveMsgBoard(arListToSend);

                string content = wechatBean.content;
                content = content.Replace("留言板", "");
                ArrayList arListTmp = UtilityHelp.Instance.getInputBarcodesAndAddr(content,new TaskBean());
                foreach(ParcelBean parcel in arListTmp)
                {
                    parcel.roomID = wechatBean.roomid;
                    parcel.fromWxID = wechatBean.from;
                }
                GetMsgBoardHelperGen getMsgGen = new GetMsgBoardHelperGen();
                Thread tMsg = new Thread(new ParameterizedThreadStart(getMsgGen.parseLeaveMsgBoard));
                tMsg.Start((object)arListTmp);
                tMsg.IsBackground = true;






            }
            catch(Exception ex)
            {

            }
        }

        private void sendFileNotice(string roomid, WechatMsgBean wechatBean)
        {
            try
            {
                string roomID = roomid;
                if (htRoomaATEmployee.ContainsKey(roomID))
                {

                    string userID = htRoomaATEmployee[roomID].ToString();


                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("收到揽收/中转指令，请跟进揽收/中转");
                    if(!wechatBean.isQQ)
                    {
                        ReplyHelper.Instance.replySuperMsg(roomID, "{$@}" + " " + sb.ToString(), "1", userID);
                    }
                    else
                    {
                        ReplyHelper.Instance.replyQQMsg(roomID, "[@" + userID + "]" + sb.ToString(), wechatBean.robotQQ);
                    }
                    

                }
            }
            catch (Exception ex)
            {

            }
        }


        private void sendATNotice(string roomid)
        {
            try
            {
                string roomID = roomid;
                if (htRoomaATEmployee.ContainsKey(roomID))
                {

                    string userID = htRoomaATEmployee[roomID].ToString();


                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("收到@指令，请跟进处理");

                    ReplyHelper.Instance.replySuperMsg(roomID, "{$@}" + " " + sb.ToString(), "1", userID);

                }
            }
            catch (Exception ex)
            {

            }
        }

        private void sendNotFindTaskToEmployee(ArrayList arListNotFindTask)
        {
            try
            {
                ParcelBean parcel = (ParcelBean)arListNotFindTask[0];
                string roomID=parcel.roomID;
                if(htRoomaATEmployee.ContainsKey(roomID))
                {

                    string userID = htRoomaATEmployee[roomID].ToString();

                    
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("以下单号没有找到跟单关键字，已通知客服跟进");
                    bool findMsg = false;
                    foreach (ParcelBean parcelBean in arListNotFindTask)
                    {
                        if (parcel.fromWxID == userID)
                        {
                            continue;
                        }
                        sb.AppendLine(parcelBean.wayBill);
                        findMsg = true;
                    }
                    if(findMsg)
                    {
                        if(!parcel.isQQMsg)
                        {
                            ReplyHelper.Instance.replySuperMsg(roomID, "{$@}" + " " + sb.ToString(), "1", userID);
                        }
                        else
                        {
                            ReplyHelper.Instance.replyQQMsg(roomID, "[@" + userID + "]" + sb.ToString(), parcel.robotQQ);
                        }
                        
                    }
                    
                }
            }
            catch(Exception ex)
            {

            }
           
        }

        private void handleCopyResponse(ArrayList arTaskDone)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" 以下单号任务重复");
                foreach (ParcelBean parcelBean in arTaskDone)
                {
                    sb.AppendLine(parcelBean.wayBill + " " + parcelBean.taskType);
                }
                ParcelBean parcel= (ParcelBean)arTaskDone[0];
                string append = "";
                if (!htRoomATClose.ContainsKey(parcel.roomID))
                {
                    if(!StoParams.Instance.isStrictMode)
                    {
                        append = "{$@}";
                    }
                    
                }
                string mentionUser = "1";
                if(!parcel.isQQMsg)
                {
                    ReplyHelper.Instance.replySuperMsg(parcel.roomID, append + sb.ToString(), mentionUser, parcel.fromWxID);
                }
                else
                {
                    ReplyHelper.Instance.replyQQMsg(parcel.roomID, sb.ToString(), parcel.robotQQ);
                }
               


            }
            catch(Exception ex)
            {

            }
        }

  
        public void doTaskLanshou(ArrayList arListTask)
        {
            try
            {
                lock (lockQueue)
                {
                    int totalCount = 0;
                    foreach(ArrayList arList in arListTask)
                    {
                        foreach(ParcelBean parcelBean in arList)
                        {
                            if(!arListLanshouFilters.Contains(parcelBean.wayBill))
                            {
                                totalCount++;
                            }
                           
                        }
                    }
                    if(arListTask.Count>0)
                    {
                        string xlsContentReply = "收到待揽收单号数量："+ totalCount.ToString()+"";
                        xlsContentReply = xlsContentReply.TrimEnd(new char[] { '\r', '\n' });
                        if (xlsContentReply.Length > 10)
                        {
                            byte[] resMsgContent = System.Text.Encoding.Default.GetBytes(xlsContentReply);
                            Program.weServer.sendContent(resMsgContent);

                        }
                    }
                    

                    ArrayList arListTaskBarcodes = new ArrayList();
                    Hashtable htRes = new Hashtable();
                    foreach(ArrayList arListData in arListTask)
                    {
                        LanshouHelperGen.Instance.parseLanshou(arListData);
                        //if(res)
                        {
                            foreach(ParcelBean parcelBean in arListData)
                            {
                                if(!arListTaskBarcodes.Contains(parcelBean.wayBill))
                                {
                                    arListTaskBarcodes.Add(parcelBean.wayBill);
                                }
                                if(!arListLanshouFilters.Contains(parcelBean.wayBill))
                                {
                                    arListLanshouFilters.Add(parcelBean.wayBill);
                                }
                            }

                        }
                    }
                    string resMsg = "";

                    resMsg = UtilityHelp.Instance.getResponse(arListTaskBarcodes, htRes);
                    LogHelper.Instance.writeLog("arListTaskBarcodes count=" + arListTaskBarcodes.Count.ToString() + " htResWechat count=" + htRes.Count.ToString()+ " resMsg="+ resMsg);
                    resMsg = resMsg.TrimEnd(new char[] { '\r', '\n' });
                    if (resMsg.Length > 13)
                    {
                        byte[] resMsgContent = System.Text.Encoding.Default.GetBytes(resMsg);
                        //Program.weServer.sendContent(resMsgContent);
                        ReplyHelper.Instance.replyMsg(resMsg, curGroupPosDefLanshou);
                        LogHelper.Instance.writeStaticBarcode(arListStatics);
                    }
                    if(arListLanshouFilters.Count>2000)
                    {
                        arListLanshouFilters.RemoveRange(0, 500);
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }


    }
}
